<?php 
  require_once dirname(realpath(__FILE__))."./../../../config/config.inc.php";
  require_once HELPER_PATH.'image.helper.php';
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript" src="/js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="/js/env.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script>
$(function() {
	var error = $('#error').text();
	var fileType = $('#fileType').text();
	var photoEncoded = $('#photoEncoded').text();
	var thumbEncoded = $('#thumbEncoded').text();

	//check max photo upload
	if (window.top.$('#photos img').length > (MAX_PHOTO_UPLOAD - 1)) {
		error = 'Bạn chỉ được upload tối đa '+MAX_PHOTO_UPLOAD+' ảnh';
	}

	//if there is an error then show the error
	if (error != '') {
		window.top.$('#upload-photo-error').text(error);
		window.top.$('#upload-loading').html('');
		return false;
	}

	// if there is photoData and fileType then show photo
	if (thumbEncoded != ''  && photoEncoded != '' && fileType != '') {
		var photo = 
			  '<span class="photo">'
			+ '<input name="photo[]" type="hidden" value="data:'+fileType+';base64,'+photoEncoded+'" />'
			+ '<img style="width:'+THUMB_WIDTH+';height:'+THUMB_HEIGHT+';" src="data:'+fileType+';base64,'+thumbEncoded+'" />'
			+ '</span>';

		window.top.$('#photos').append(photo);

	// if there is no data photo then show error
	} else {
		window.top.$('#upload-photo-error').text('Có lỗi xảy ra khi tải ảnh.');
	}

	//remove the loading icon
	window.top.$('#upload-loading').html('');

	//if there is atleast one photo uploaded then show the note
	if (window.top.$('#photos span.photo').length > 0) {
		window.top.$('#note-delete-photo').html('Bạn có thể kích đúp lên ảnh để xóa ảnh.<br />*Ảnh đầu tiên sẽ được chọn làm ảnh đại diện.');
	}
});
</script>
</head>
<body>
<?php var_dump($_FILES['photo']);
  $fileName = trim($_FILES['photo']['name']);
  $fileType = $_FILES['photo']['type'];
  $fileTmpName = $_FILES['photo']['tmp_name'];
  $fileError = $_FILES['photo']['error'];
  $fileSize = $_FILES['photo']['size'];

  $validFiles = array('image/jpg','image/jpeg','image/png', 'image/pjpeg');

  #file size is not valid
  if ($fileSize > MAX_SIZE_PHOTO_UPLOAD || $fileSize == 0) {
     echo '<div id="error">Kích thước file ảnh không hợp lệ</div>';
  
  #file type is not valid
  } elseif(!in_array($fileType, $validFiles)) {
    echo '<div id="error">Định dạng file ảnh không hợp lệ</div>';

  #everything is ok then upload the photo
  } else {
    echo '<div id="fileType">'.$fileType.'</div>'; 
    
    # generate thumb
    $thumbData = imageGenImage($fileTmpName, $fileType, '', 'jpeg', THUMB_WIDTH, THUMB_HEIGHT);
    echo '<div id="thumbEncoded">'.base64_encode($thumbData).'</div>';
    
    # generate photo
    $photoData = imageGenImage($fileTmpName, $fileType, '', 'jpeg', PHOTO_WIDTH, PHOTO_HEIGHT);
    echo '<div id="photoEncoded">'.base64_encode($photoData).'</div>';
  }
?>
</body>
</html>